<template>
    <div class="app-container">
      <el-form
        :model="queryParams"
        ref="queryForm"
        :inline="true"
        v-show="showSearch"
        label-width="80px"
      >
        <el-form-item label="物料编码" prop="materialCode">
          <el-input
            v-model="queryParams.materialCode"
            placeholder="请输入物料编码"
            clearable
            size="small"
            style="width: 200px"
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="物料名称" prop="materialName">
          <el-input
            v-model="queryParams.materialName"
            placeholder="请输入物料名称"
            clearable
            size="small"
            style="width: 200px"
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="物料类型" prop="materialType">
          <el-select
            v-model="queryParams.materialType"
            placeholder="请选择物料类型"
            style="width: 200px"
          >
            <el-option
              v-for="dict in materialTypeOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="物料描述" prop="materialDescribe">
          <el-input
            v-model="queryParams.materialDescribe"
            placeholder="请输入物料描述"
            clearable
            size="small"
            style="width: 200px"
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="物料分组" prop="materialGroup">
          <el-select
            v-model="queryParams.materialGroup"
            placeholder="请选择物料分组"
            style="width: 200px"
          >
            <el-option
              v-for="dict in materialGroupOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="是否追述" prop="isTracing">
          <el-select
            v-model="queryParams.isTracing"
            placeholder="请选择是否追述"
            clearable
            size="small"
            style="width: 200px"
          >
            <el-option
              v-for="dict in isTracingOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="供应商" prop="supplierId">
          <el-select
            v-model="queryParams.supplierId"
            filterable
            placeholder="请选择供应商"
            style="width: 200px"
          >
            <el-option
              v-for="item in supplierIdOptions"
              :key="item.supplierId"
              :label="item.supplierName"
              :value="item.supplierId"
            >
              <span style="float: left">{{ item.supplierCode }}</span>
              <span style="float: right; color: #8492a6; font-size: 13px">{{ item.supplierName }}</span>
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button
            type="cyan"
            icon="el-icon-search"
            size="mini"
            @click="handleQuery"
          >搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>
  
      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button
            type="primary"
            icon="el-icon-plus"
            size="mini"
            @click="handleAdd"
            v-hasPermi="['system:material:add']"
          >新增</el-button>
        </el-col>
        <!-- <el-col :span="1.5">
          <el-button
            type="success"
            icon="el-icon-edit"
            size="mini"
            :disabled="single"
            @click="handleUpdate"
            v-hasPermi="['system:material:edit']"
          >修改</el-button>
        </el-col> -->
        <el-col :span="1.5">
          <el-button
            type="danger"
            icon="el-icon-delete"
            size="mini"
            :disabled="multiple"
            @click="handleDelete"
            v-hasPermi="['system:material:remove']"
          >删除</el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="warning"
            icon="el-icon-download"
            size="mini"
            @click="handleExport"
            v-hasPermi="['system:material:export']"
          >导出</el-button>
        </el-col>
        <right-toolbar
          :showSearch.sync="showSearch"
          @queryTable="getList"
        ></right-toolbar>
      </el-row>
  
      <el-table
        v-loading="loading"
        :data="materialList"
        @selection-change="handleSelectionChange"
        style="width: 100%"
      >
        <el-table-column type="selection" width="60" align="center" fixed="left" />
        <el-table-column label="物料主键id" align="center" prop="materialId" width="160" fixed="left" v-if="false" />
        <el-table-column label="物料编码" align="center" prop="materialCode" width="100" fixed="left" />
        <el-table-column label="物料名称" align="center" prop="materialName" width="200" />
        <el-table-column label="物料类型" align="center" prop="materialType" :formatter="materialTypeFormat" width="100" />
        <el-table-column label="物料描述" align="center" prop="materialDescribe" width="200" />
        <el-table-column label="物料分组" align="center" prop="materialGroup" :formatter="materialGroupFormat" width="100" />
        <el-table-column label="物料单位" align="center" prop="materialUnit" width="100" />
        <el-table-column label="物料特征值" align="center" prop="materialFace" width="120" />
        <el-table-column label="是否追述" align="center" prop="isTracing" :formatter="isTracingFormat" width="100" />
        <el-table-column label="供应商" align="center" prop="supplierId" :formatter="supplierIdFormat" width="200" />
        <el-table-column label="有效时间" align="center" prop="validTime" width="100" />
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="200" fixed="right">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="text"
              icon="el-icon-edit"
              style="margin-right: 9px"
              @click="handleUpdate(scope.row)"
              v-hasPermi="['system:material:edit']"
            >修改</el-button>
            <el-button
              size="mini"
              type="text"
              icon="el-icon-delete"
              @click="handleDelete(scope.row)"
              v-hasPermi="['system:material:remove']"
            >删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <pagination
        v-show="total > 0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />
  
      <!-- 添加或修改物料信息对话框 -->
      <el-dialog :title="title" :visible.sync="open" width="760px" append-to-body>
        <el-form ref="form" :model="form" :rules="rules" label-width="90px">
          <el-row :gutter="20">
            <el-col :lg="12">
              <el-form-item label="物料编码" prop="materialCode">
                <el-input
                  v-model="form.materialCode"
                  placeholder="请输入物料编码"
                  style="width: 240px"
                />
              </el-form-item>
            </el-col>
            <el-col :lg="12">
              <el-form-item label="物料名称" prop="materialName">
                <el-input
                  v-model="form.materialName"
                  placeholder="请输入物料名称"
                  style="width: 240px"
                />
              </el-form-item>
            </el-col>
            <el-col :lg="12">
              <el-form-item label="物料类型" prop="materialType">
                <el-select
                  v-model="form.materialType"
                  placeholder="请选择物料类型"
                  style="width: 240px"
                >
                  <el-option
                    v-for="dict in materialTypeOptions"
                    :key="dict.dictValue"
                    :label="dict.dictLabel"
                    :value="dict.dictValue"
                  ></el-option>
                </el-select>
              </el-form-item>
            </el-col>
  
            <el-col :lg="12">
              <el-form-item label="物料分组" prop="materialGroup">
                <el-select
                  v-model="form.materialGroup"
                  placeholder="请选择物料分组"
                  style="width: 240px"
                >
                  <el-option
                    v-for="dict in materialGroupOptions"
                    :key="dict.dictValue"
                    :label="dict.dictLabel"
                    :value="dict.dictValue"
                  ></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :lg="12">
              <el-form-item label="物料单位" prop="materialUnit">
                <el-input
                  v-model="form.materialUnit"
                  placeholder="请输入物料单位"
                  style="width: 240px"
                />
              </el-form-item>
              <el-form-item label="物料特征值" prop="materialFace">
                <el-input
                  v-model="form.materialFace"
                  placeholder="请输入物料特征值"
                  style="width: 240px"
                />
              </el-form-item>
            </el-col>
            <el-col :lg="12">
              <el-form-item label="是否追述" prop="isTracing">
                <el-select v-model="form.isTracing" placeholder="请选择是否追述" style="width: 240px">
                  <el-option
                    v-for="dict in isTracingOptions"
                    :key="dict.dictValue"
                    :label="dict.dictLabel"
                    :value="dict.dictValue"
                  ></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :lg="12">
              <el-form-item label="供应商" prop="supplierId">
                <el-select
                  v-model="form.supplierId"
                  filterable
                  placeholder="请选择供应商"
                  style="width: 240px"
                >
                  <el-option
                    v-for="item in supplierIdOptions"
                    :key="item.supplierId"
                    :label="item.supplierName"
                    :value="item.supplierId"
                  >
                    <span style="float: left">{{ item.supplierCode }}</span>
                    <span style="float: right; color: #8492a6; font-size: 13px">{{ item.supplierName }}</span>
                  </el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :lg="12">
              <el-form-item label="有效时间" prop="validTime">
                <el-input
                  type="number"
                  v-model="form.validTime"
                  placeholder="请输入有效时间"
                  style="width: 240px"
                />
              </el-form-item>
            </el-col>
            <el-col :lg="12">
              <el-form-item label="物料描述" prop="materialDescribe">
                <el-input
                  v-model="form.materialDescribe"
                  placeholder="请输入物料描述"
                  type="textarea"
                  :rows="4"
                  style="width: 240px"
                />
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </el-dialog>
    </div>
  </template>
  
  <script>
  import {
    listMaterial,
    getMaterial,
    delMaterial,
    addMaterial,
    updateMaterial,
    exportMaterial,
  } from "@/api/base/material";
  import { listAllSupplier } from "@/api/base/supplier";
  
  export default {
    name: "Material",
    data() {
      return {
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 物料信息表格数据
        materialList: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        // 物料类型字典
        materialTypeOptions: [],
        // 物料分组字典
        materialGroupOptions: [],
        // 是否追述字典
        isTracingOptions: [],
        // 供应商字典
        supplierIdOptions: [],
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          materialCode: null,
          materialName: null,
          materialType: null,
          materialDescribe: null,
          materialGroup: null,
          materialUnit: null,
          materialFace: null,
          isTracing: null,
          supplierId: null,
          validTime: null,
        },
        // 表单参数
        form: {},
        // 表单校验
        rules: {
          materialCode: [
            { required: true, message: "物料编码不能为空", trigger: "blur" },
          ],
          materialName: [
            { required: true, message: "物料名称不能为空", trigger: "blur" },
          ],
          materialGroup: [
            { required: true, message: "物料分组不能为空", trigger: "blur" },
          ],
          materialUnit: [
            { required: true, message: "物料单位不能为空", trigger: "blur" },
          ],
        },
      };
    },
    created() {
      this.getList();
      this.getDicts("mes_material_type").then((response) => {
        this.materialTypeOptions = response.data;
      });
      this.getDicts("mes_material_team").then((response) => {
        this.materialGroupOptions = response.data;
      });
      this.getDicts("sys_yes_no").then((response) => {
        this.isTracingOptions = response.data;
      });
      listAllSupplier().then((response) => {
        this.supplierIdOptions = response;
      });
    },
    methods: {
      /** 查询物料信息列表 */
      getList() {
        this.loading = true;
        listMaterial(this.queryParams).then((response) => {
          this.materialList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      },
      // 物料类型字典翻译
      materialTypeFormat(row, column) {
        return this.selectDictLabel(this.materialTypeOptions, row.materialType);
      },
      // 物料分组字典翻译
      materialGroupFormat(row, column) {
        return this.selectDictLabel(this.materialGroupOptions, row.materialGroup);
      },
      // 是否追述字典翻译
      isTracingFormat(row, column) {
        return this.selectDictLabel(this.isTracingOptions, row.isTracing);
      },
      // 供应商字典翻译
      supplierIdFormat(row, column) {
        var actions = [];
        Object.keys(this.supplierIdOptions).some((key) => {
          if (this.supplierIdOptions[key].supplierId == "" + row.supplierId) {
            actions.push(this.supplierIdOptions[key].supplierName);
            return true;
          }
        });
        return actions.join("");
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          materialId: null,
          materialCode: null,
          materialName: null,
          materialType: null,
          materialDescribe: null,
          materialGroup: null,
          materialUnit: null,
          materialFace: null,
          isTracing: null,
          supplierId: null,
          validTime: null,
          createBy: null,
          createTime: null,
          updateBy: null,
          updateTime: null,
        };
        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map((item) => item.materialId);
        this.single = selection.length !== 1;
        this.multiple = !selection.length;
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset();
        this.open = true;
        this.title = "添加物料信息";
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset();
        const materialId = row.materialId || this.ids;
        getMaterial(materialId).then((response) => {
          this.form = response.data;
          this.open = true;
          this.title = "修改物料信息";
        });
      },
      /** 提交按钮 */
      submitForm() {
        this.$refs["form"].validate((valid) => {
          if (valid) {
            if (this.form.materialId != null) {
              updateMaterial(this.form).then((response) => {
                if (response.code === 200) {
                  this.msgSuccess("修改成功");
                  this.open = false;
                  this.getList();
                }
              });
            } else {
              addMaterial(this.form).then((response) => {
                if (response.code === 200) {
                  this.msgSuccess("新增成功");
                  this.open = false;
                  this.getList();
                }
              });
            }
          }
        });
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const materialIds = row.materialId || this.ids;
        this.$confirm(
          '是否确认删除物料信息编号为"' + materialIds + '"的数据项?',
          "警告",
          {
            confirmButtonText: "确定",
            cancelButtonText: "取消",
            type: "warning",
          }
        )
          .then(function () {
            return delMaterial(materialIds);
          })
          .then(() => {
            this.getList();
            this.msgSuccess("删除成功");
          })
          .catch(function () {});
      },
      /** 导出按钮操作 */
      handleExport() {
        const queryParams = this.queryParams;
        this.$confirm("是否确认导出所有物料信息数据项?", "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        })
          .then(function () {
            return exportMaterial(queryParams);
          })
          .then((response) => {
            this.download(response.msg);
          })
          .catch(function () {});
      },
    },
  };
  </script>